package cn.bopomo.dealer.base.action.ajax;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.vo.ModuleList;

import com.metaparadigm.jsonrpc.JSONSerializer;

public class LoadModuleAjax extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//AJAX返回设置
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		//获取参数
		Integer mId = new Integer(request.getParameter("mid"));
		//获取Service
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		//获取"模块"实例
		ModuleList module = os.getModule(mId);
		//
		if (module != null){
			//JSONArray  array = this.jsonProblems(module);
			//建立JSON实例
			JSONObject json = new JSONObject();
			//JSONSerializer js = new JSONSerializer();
			//js.registerDefaultSerializers();
			//js.toJSON(module);
			//写入JSON属性
			json.put("moduleId", module.getMid());
			json.put("moduleName", module.getModuleName());
			json.put("moduleAction", module.getModuleAction());
			json.put("moduleTag", module.getModuleTag());
			json.put("addDate", module.getAddDate().toLocaleString());
			//返回JSON对象
			response.getWriter().write(json.toString());
			return null;
		}else{
			response.getWriter().write("");
			return null;
		}
		
	}

}
